// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3,4,5 - The three creations it can summon. GIVE THE IN-GAME NUMBER
//   Cell 6 - Talking node

begincreaturescript;

variables;

short i,target;
short last_spawn;
short r1;
short last_abil;
short used_abil;
short num_shaped = 0;
short mess = 0;
short summ = 0;

body;

beginstate INIT_STATE;
	last_spawn = get_current_tick();
	last_abil = get_current_tick();
	
	set_resistance(ME,7,90);
	
	set_name(ME,"Gyllaran");
	set_level(ME,30);
	change_max_health(ME,100);
	set_boss_level(ME,2);
	set_aggression(ME,6);
	
	break;

beginstate DEAD_STATE;
	sf(32,5,1);
	erase_char(66);
	erase_char(67);
	begin_talk_mode(14);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
			set_state(3);
		}
	
	if (get_foe_target(ME,8,0)) {
		if (mess == 0) {
			mess = 1;
			begin_talk_mode(12);
			}
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		if (mess == 0) {
			mess = 1;
			begin_talk_mode(12);
			}
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (my_dist_from_start() >= 6) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0)
			fidget(ME,25);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((get_health(ME) < get_max_health(ME) - 200) && 
	  (get_nearest_party_char(8) >= 0) && 
	  (summ == 0)) {
		summ = 1;
		
		summon_creature(58);
		set_name(66,"Spectral Servant");
		set_level(66,20);
		place_particle_num(66,10,7,10);
		set_resistance(66,0,99);
		set_resistance(66,1,99);
		set_resistance(66,2,99);
		set_resistance(66,3,99);
		set_resistance(66,4,99);
		set_resistance(66,5,99);
		set_resistance(66,6,99);
		
		summon_creature(59);
		set_name(67,"Spectral Servant");
		set_level(67,20);
		place_particle_num(67,10,7,10);
		set_resistance(67,0,99);
		set_resistance(67,1,99);
		set_resistance(67,2,99);
		set_resistance(67,3,99);
		set_resistance(67,4,99);
		set_resistance(67,5,99);
		set_resistance(67,6,99);
		begin_talk_mode(13);
		}

	used_abil = 0;
	if ( (tick_difference(last_abil,get_current_tick()) > 0) && (is_combat()) && (get_nearest_party_char(8) >= 0)) {
		if ((get_char_status(ME,1) <= 0) && (get_ran(1,0,100) < 40)) {
			run_char_animation(2,1,35);	
			print_named_str(ME,"starts to move faster!");
			pc_heard_sound_delay(101,250);						
		  	place_particle_num(ME,3,0,8);
			set_char_status(ME,1,5);
			last_abil = get_current_tick();
			used_abil = 1;
			}
			else if ((get_char_status(ME,8) <= 0) && (get_ran(1,0,100) < 20)) {
				run_char_animation(2,1,35);	
				print_named_str(ME,"creates a magical shield.");
				pc_heard_sound_delay(101,250);						
		 	 	place_particle_num(ME,1,0,8);
				set_char_status(ME,8,6);
				last_abil = get_current_tick();
				used_abil = 1;
				}

		}
	if (used_abil)
		end();
		
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(6) == 0) {
		print_str("Talking: You make a bit of small talk, but you don't learn");
		print_str("  anything interesting.");
		}
		else begin_talk_mode(get_memory_cell(6));
break;